<template>
	<view class="goods_list" :style="page_height ? 'height:' + page_height : ''">
		<view class="search">
			<view class="inputText">
				<image src="../../../static/search.png"></image>
				<input type="text" placeholder="请输入关键词" confirm-type="search" @confirm="search" v-model="keyword" />
			</view>
		</view>
		<view class="main">
			<scroll-view class="goods_left" scroll-y="true" scroll-with-animation :scroll-into-view="'cate_' + cate_active">
				<view
					v-for="(item, key) in cate"
					@tap="cate_tap(item.value, key)"
					:data-cate-id="item.value"
					:id="'cate_' + key"
					:key="key"
					:data-index="key"
					:class="['goods_cate', { active: cate_active == key }]"
				>
					<!-- {{item.title | cutName }} -->
					{{ item.name }}
				</view>
			</scroll-view>
			<scroll-view class="goods_right" scroll-y="true" @scrolltolower="next_page">
				<view class="banner" v-if="ad.length > 0">
					<swiper class="swiper" :autoplay="true" :interval="2000" :duration="500">
						<swiper-item v-for="(item, key) in ad" :key="key">
							<navigator :url="item.target"><image :src="item.thumb" mode="aspectFill"></image></navigator>
						</swiper-item>
					</swiper>
				</view>
				<view class="empty_goods" v-if="dataList.length <= 0">
					<image src="http://boweisou.oss-cn-shenzhen.aliyuncs.com/5G/hotgoods.png" mode="widthFix"></image>
					<text>暂无商品</text>
				</view>
				<view class="uni-product-list">
					<view class="uni-product" v-for="(item, index) in dataList" :key="index" @tap="into_goods_detail(item.id)">
						<view class="image-view"><image class="uni-product-image" :src="item.thumb"></image></view>
						<view class="uni-product-title">{{ item.name }}</view>
						<view class="uni-product-price">
							<text class="uni-product-price-original">{{ item.money }}万</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			id: null,
			keyword: '',
			cate: [],
			cate_active: 0,
			goods: [],
			page: 1,
			page_size: 15,
			page_height: false,
			dataList: [],
			cid: 1,
			ad: []
		};
	},
	filters: {
		cutName: function(value) {
			if (value.length > 7) {
				return value.substring(0, 7);
			} else {
				return value;
			}
		}
	},
	onLoad() {
		this.id = 1;
		let systemInfo = uni.getSystemInfoSync();
		this.page_height = systemInfo.windowHeight + 'px';
	},
	onShow() {
		this.loadData();
	},
	methods: {
		async loadData() {
			let cate_res = await this.$request.post('/landed/getShopCategory', {
				data: {
					id: this.id,
					cid: this.cid,
					page: this.page,
					page_size: this.page_size
				}
			});
			this.cate = cate_res.data.data.list.shop_category;
			this.ad = cate_res.data.data.list.ad;
			let goods = this.page == 1 ? [] : this.goods;
			this.dataList.push(...cate_res.data.data.list.list);
		},
		cate_tap(res, index) {
			this.cid = res;
			this.dataList = [];
			this.page = 1;
			this.cate_active = index;
			this.loadData();
		},
		next_page() {
			this.page++;
			this.loadData(this.cid);
		},
		search() {
			// uni.navigateTo({
			// 	url:'/pages/goods/search/search?keyword='+this.keyword
			// })
		},
		into_goods_detail(goodsid) {
			let url = '';
			if (this.cid == 1) {
				url = '/pages/house_property/propertiesdetail/propertiesdetail?id=';
			} else if (this.cid == 2) {
				url = '/pages/house_property/seconddetail/seconddetail?id=';
			} else if (this.cid == 3) {
				url = '/pages/house_property/rentingdetail/rentingdetail?id=';
			}
			uni.navigateTo({
				url: url + goodsid
			});
		}
	}
};
</script>

<style lang="scss">
/* product */
page {
	background: #f8f8f8;
	height: 100%;
}

view {
	font-size: $uni-font-size-base;
}

.goods_list {
	width: 100%;
	height: 100%;
	padding-top: 150upx;
	box-sizing: border-box;
	.search {
		width: 100%;
		height: 150upx;
		background-color: #ffffff;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		position: absolute;
		top: 0;
		left: 0;
		.inputText {
			width: 640upx;
			height: 70upx;
			position: relative;
			border-radius: 70upx;
			overflow: hidden;
			image {
				z-index: 100;
				width: 35upx;
				height: 35upx;
				position: absolute;
				top: 50%;
				left: 20upx;
				transform: translateY(-50%);
			}
			input {
				width: 100%;
				height: 100%;
				box-sizing: border-box;
				background-color: #f3f3f3;
				padding-left: 70upx;
				color: $uni-text-color-inverse;
				font-size: $uni-font-size-base;
			}
		}
	}
	.main {
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: flex-start;
		position: relative;
		.goods_left {
			width: 160upx;
			height: 100%;
			background-color: #F3F3F3;
			.goods_cate {
				width: 100%;
				height: 90upx;
				line-height: 90upx;
				box-sizing: border-box;
				text-align: center;
				font-size: $uni-font-size-base;
				color: #838383;
				&.active {
					background-color: $uni-bg-color;
					position: relative;
					color: $uni-text-color;
					&::before {
						display: block;
						content: '';
						width: 10upx;
						height:40upx;
						border-radius: 10upx;
						position: absolute;
						top: 25upx;
						left: 0;
						background-color: #F34B32;
					}
				}
			}
		}
		.goods_right {
			width: calc(100% - 160upx);
			height: 100%;
			background-color: $uni-bg-color;
			.banner {
				width: 100%;
				height: 240upx;
				box-sizing: border-box;
				padding: 15upx;
				.swiper {
					width: 100%;
					height: 100%;
					image {
						height: 240upx;
					}
				}
			}
			.empty_goods {
				text-align: center;
				margin-top: 30%;

				image {
					width: 95upx;
					height: 94upx;
				}

				text {
					display: block;
					color: #999999;
					font-size: $uni-font-size-base;
				}
			}
			.uni-product-list {
				display: flex;
				width: 100%;
				flex-wrap: wrap;
				flex-direction: row;
				.uni-product {
					padding: 20upx;
					display: flex;
					flex-direction: column;
					.image-view {
						height: 250upx;
						width: 250upx;
						margin: 12upx 0;
					}
					.uni-product-image {
						height: 250upx;
						width: 250upx;
					}
					.uni-product-title {
						width: 250upx;
						word-break: break-all;
						display: -webkit-box;
						overflow: hidden;
						line-height: 1.5;
						text-overflow: ellipsis;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						font-size: $uni-font-size-base;
					}
					.uni-product-price {
						margin-top: 10upx;
						font-size: 28upx;
						line-height: 1.5;
						position: relative;
						.uni-product-price-original {
							color: #F33632;
						}
						.uni-product-price-favour {
							color: #888888;
							text-decoration: line-through;
							margin-left: 10upx;
						}
						.uni-product-tip {
							position: absolute;
							right: 10upx;
							background-color: #ff3333;
							color: #ffffff;
							padding: 0 10upx;
							border-radius: 5upx;
						}
					}
				}
			}
		}
	}
}
</style>
